package com.srv.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	public static boolean isEmpty(String $srcStr) {
        return ($srcStr == null || $srcStr.length() < 1);
    }

    public static String nullToblank(String $srcStr) {
        return nullToblank($srcStr, "");
    }

    public static String appendZore(long l, int length) {
        String appendStr = multiStrings("0", length) + l;
        return appendStr.substring(appendStr.length() - length);
    }

    public static String appendZore(int l, int length) {
        String appendStr = multiStrings("0", length) + l;
        return appendStr.substring(appendStr.length() - length);
    }

    public static String appendZore(String s, int length) {
        String appendStr = multiStrings("0", length) + s;
        return appendStr.substring(appendStr.length() - length);
    }

    private static String multiStrings(String s, int length) {
        StringBuffer strBuf = new StringBuffer();

        for (int i = 0; i < length; i++) {
            strBuf.append(s);
        }

        return strBuf.toString();
    }

    public static String nullToblank(String $srcStr, String $defaultValue) {
        if ($srcStr == null || $srcStr.equals("")) {
            if ($defaultValue == null) {
                return "";
            } else {
                return $defaultValue;
            }
        } else {
            return $srcStr.trim();
        }
    }

    public static boolean isInCharPool(String $srcStr, String $charPool) {
        return isInCharPool($srcStr, $charPool, true);
    }

    public static boolean isInCharPool(String $srcStr, String $charPool, boolean $caseNotIgnore) {
        if ($srcStr == null || "".equals($srcStr)) {
            return true;
        }

        if ($charPool == null || "".equals($charPool)) {
            return false;
        }

        String _srcStr = $srcStr;
        String _charPool = $charPool;

        if (!$caseNotIgnore) {
            _srcStr = _srcStr.toUpperCase();
            _charPool = _charPool.toUpperCase();
        }

        for (int i = 0; i < _srcStr.length(); i++) {
            if (_charPool.indexOf(_srcStr.charAt(i)) < 0) {
                return false;
            }
        }

        return true;
    }

    public static String regReplace(String $srcStr, String $pattern, String $destRep, int $caseIgnore) {
        if ($srcStr != null && !$srcStr.equals("")) {
            Pattern _pattern = Pattern.compile($pattern, $caseIgnore);
            Matcher _matcher = _pattern.matcher($srcStr);
            StringBuffer _strBuf = new StringBuffer();

            boolean _findFlag = _matcher.find();

            while (_findFlag) {
                _matcher.appendReplacement(_strBuf, $destRep);
                _findFlag = _matcher.find();
            }
            _matcher.appendTail(_strBuf);

            return _strBuf.toString();
        } else {
            return "";
        }
    }

    public static String replace(String $srcStr, String $replaceStr, String $destRep, int $caseIgnore) {
        return regReplace($srcStr, $replaceStr, $destRep, $caseIgnore);
    }

    public static List<Long> listStringToLong(List<String> ls) {
        List<Long> ll = new ArrayList<Long>();

        for (String s : ls) {
            ll.add(Long.parseLong(s));
        }

        return ll;
    }

    public static long ipToLong(String ip) {
        String[] ips = ip.split("[.]");

        return 256 * 256 * 256 * Long.parseLong(ips[0]) + 256 * 256 * Long.parseLong(ips[1])
                + 256 * Long.parseLong(ips[2]) + Long.parseLong(ips[3]);
    }

    public static void main(String[] args) {

        System.out.println(replace("sdj123fs123df s123dfs123df sd f12sd34fs", "123",
                "321", 1));

    }
}
